package com.zhouchao.email.common;

import lombok.Data;

/**
 * @Author: zhouchao
 * @Date: 2021/10/15 21:52
 * @Description:
 */
@Data
public class Result<T> {

    /**
     * code=0，代表成功
     */
    private String code;

    /**
     * 描述信息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    // private String traceId = MDC.get("traceId");

    private Result(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功
     *
     * @return
     */
    public static <T> Result<T> success() {
        return handleRs("0","success", null);
    }

    /**
     * 成功
     *
     * @param data
     * @return
     */
    public static <T> Result<T> success(T data) {
        return handleRs("0","success", data);
    }

    /**
     * 成功
     * @param msg
     * @param data
     * @return
     */
    public static <T> Result<T> success(String msg, T data) {
        return handleRs("0", msg, data);
    }


    /**
     * 失败
     *
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static <T> Result<T> failure(String code, String msg, T data) {
        return handleRs(code, msg, data);
    }


    private static <T> Result<T> handleRs(String code, String msg, T data) {
        return new Result(code, msg, data);
    }

}
